package com.uls.web.controller.questionnaire;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.uls.common.annotation.Log;
import com.uls.common.core.controller.BaseController;
import com.uls.common.core.domain.AjaxResult;
import com.uls.common.enums.BusinessType;
import com.uls.questionnaire.domain.QuestionnaireStatistics;
import com.uls.questionnaire.service.IQuestionnaireStatisticsService;
import com.uls.common.utils.poi.ExcelUtil;
import com.uls.common.core.page.TableDataInfo;

/**
 * 问卷结果统计Controller
 * 
 * @author zxh
 * @date 2024-11-23
 */
@RestController
@RequestMapping("/questionnaire/statistics")
public class QuestionnaireStatisticsController extends BaseController
{
    @Autowired
    private IQuestionnaireStatisticsService questionnaireStatisticsService;

    /**
     * 查询问卷结果统计列表
     */
    @PreAuthorize("@ss.hasPermi('questionnaire:statistics:list')")
    @GetMapping("/list")
    public TableDataInfo list(QuestionnaireStatistics questionnaireStatistics,@RequestParam(required = false, defaultValue = "10") Integer limit)
    {
        startPage();
        questionnaireStatisticsService.updateStatistics();
        List<QuestionnaireStatistics> list = questionnaireStatisticsService.selectQuestionnaireStatisticsList(questionnaireStatistics, limit);
        return getDataTable(list);
    }

    /**
     * 导出问卷结果统计列表
     */
    @PreAuthorize("@ss.hasPermi('questionnaire:statistics:export')")
    @Log(title = "问卷结果统计", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QuestionnaireStatistics questionnaireStatistics,@RequestParam(defaultValue = "10") Integer limit)
    {
        List<QuestionnaireStatistics> list = questionnaireStatisticsService.selectQuestionnaireStatisticsList(questionnaireStatistics,limit);
        ExcelUtil<QuestionnaireStatistics> util = new ExcelUtil<QuestionnaireStatistics>(QuestionnaireStatistics.class);
        util.exportExcel(response, list, "问卷结果统计数据");
    }

    /**
     * 获取问卷结果统计详细信息
     */
    @PreAuthorize("@ss.hasPermi('questionnaire:statistics:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(questionnaireStatisticsService.selectQuestionnaireStatisticsById(id));
    }

    /**
     * 新增问卷结果统计
     */
    @PreAuthorize("@ss.hasPermi('questionnaire:statistics:add')")
    @Log(title = "问卷结果统计", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QuestionnaireStatistics questionnaireStatistics)
    {
        return toAjax(questionnaireStatisticsService.insertQuestionnaireStatistics(questionnaireStatistics));
    }

    /**
     * 修改问卷结果统计
     */
    @PreAuthorize("@ss.hasPermi('questionnaire:statistics:edit')")
    @Log(title = "问卷结果统计", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QuestionnaireStatistics questionnaireStatistics)
    {
        return toAjax(questionnaireStatisticsService.updateQuestionnaireStatistics(questionnaireStatistics));
    }

    /**
     * 删除问卷结果统计
     */
    @PreAuthorize("@ss.hasPermi('questionnaire:statistics:remove')")
    @Log(title = "问卷结果统计", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(questionnaireStatisticsService.deleteQuestionnaireStatisticsByIds(ids));
    }


    @GetMapping("/statistics/{questionnaireId}")
    public List<QuestionnaireStatistics> getStatistics(@PathVariable Long questionnaireId) {
        return questionnaireStatisticsService.getStatistics(questionnaireId);
    }

}
